package io.yuncheng.modules.sys.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import io.yuncheng.common.annotation.SysLog;
import io.yuncheng.common.config.DictConfig;
import io.yuncheng.common.utils.Constant;
import io.yuncheng.common.validator.ValidatorUtils;
import io.yuncheng.modules.sys.entity.SubApplicationEntityVo;
import io.yuncheng.modules.sys.entity.SubApplicationVo;
import io.yuncheng.modules.sys.entity.SysDeptEntity;
import io.yuncheng.modules.sys.service.SysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.yuncheng.modules.sys.entity.SubApplicationEntity;
import io.yuncheng.modules.sys.service.SubApplicationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import io.yuncheng.common.utils.PageUtils;
import io.yuncheng.common.utils.R;

import javax.annotation.Resource;


/**
 * 应用表
 *
 * @author Mark
 * @email 15721509625@163.com
 * @date 2020-10-10 13:47:44
 */
@RestController
@Api(tags="应用管理")
@RequestMapping("sys/subapplication")
public class SubApplicationController {
    @Autowired
    private SubApplicationService subApplicationService;

    @Autowired
    private SysDeptService sysDeptService;

    @Resource
    private DictConfig dictConfig;


    /**
     * 列表
     */
    @PostMapping("/list")
    @ApiOperation("列表")
    @RequiresPermissions("sys:subapplication:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = subApplicationService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 查询有效的列表
     * @param params
     * @return
     */
    @PostMapping("/list-confirm")
    @ApiOperation("有效列表")
    @RequiresPermissions("sys:subapplication:list")
    public R listConfirm(@RequestParam Map<String, Object> params){
        PageUtils page = subApplicationService.queryConfirmPage(params);
        return R.ok().put("page", page);
    }
    @PostMapping("/list-confirm-permissions")
    @ApiOperation("过滤权限的有效列表")
    @RequiresPermissions("sys:subapplication:list")
    public R listConfirmTest(@RequestParam Map<String, Object> params){
        List<SubApplicationEntityVo> subApplicationEntities = subApplicationService.queryConfirmList(params);
        return R.ok().put("data", subApplicationEntities);
    }
    /**
     * 查询有效的列表
     * @param deptId
     * @return
     */
    @PostMapping("/by-dept")
    @ApiOperation("根据部门查询")
    @RequiresPermissions("sys:subapplication:list")
    public R listConfirm(Long deptId){
        SubApplicationEntity page = subApplicationService.queryByDeptId(deptId);
        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{appId}")
    @ApiOperation("单条")
    @RequiresPermissions("sys:subapplication:info")
    public R info(@PathVariable("appId") Long appId){
        SubApplicationEntity subApplication = subApplicationService.getById(appId);
        SysDeptEntity parent = sysDeptService.getById(subApplication.getParentDeptId());
        String s = JSON.toJSONString(subApplication);
        SubApplicationVo subApplicationVo = JSON.parseObject(s, SubApplicationVo.class);
        subApplicationVo.setParentDeptId(parent.getDeptId());
        subApplicationVo.setParentDeptName(parent.getName());
        return R.ok().put("subApplication", subApplication);
    }

    /**
     * 保存
     */
    @SysLog("资源注册")
    @PostMapping("/save")
    @ApiOperation("保存")
    @RequiresPermissions("sys:subapplication:save")
    public R save(@RequestBody SubApplicationEntity subApplication){
        subApplicationService.saveAppAndDept(subApplication);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改")
    @RequiresPermissions("sys:subapplication:update")
    public R update(@RequestBody SubApplicationEntity subApplication){
        // TODO 变更上级部门的同时将部门id的上级修改
        ValidatorUtils.validateEntity(subApplication);

        SysDeptEntity byId = sysDeptService.getById(subApplication.getParentDeptId());
        sysDeptService.checkDept(byId.getParentId(),"的数据");
        SysDeptEntity sysDeptEntity = new SysDeptEntity();
        sysDeptEntity.setDeptId(subApplication.getDeptId());
        sysDeptEntity.setName(subApplication.getAppName());
        sysDeptService.updateById(sysDeptEntity);
        subApplicationService.updateById(subApplication);
        return R.ok();
    }

    /**
     * 注销
     */
    @PostMapping("/write-off")
    @ApiOperation("注销")
    @RequiresPermissions("sys:subapplication:update")
    public R updateOff(Long appId){
        subApplicationService.writeOnAndOff(appId,Constant.StatusType.OFF);
        return R.ok();
    }

    /**
     * 注销
     */
    @PostMapping("/write-on")
    @ApiOperation("启用")
    @RequiresPermissions("sys:subapplication:update")
    public R updateOn(Long appId){
        subApplicationService.writeOnAndOff(appId,Constant.StatusType.NORMAL);
        return R.ok();
    }


    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除")
    @RequiresPermissions("sys:subapplication:delete")
    public R delete(@RequestBody Long[] appIds){
        subApplicationService.removeByIds(Arrays.asList(appIds));
        List<Long>  list = new ArrayList<>();
        for (Long appId : appIds) {
            SubApplicationEntity byId = subApplicationService.getById(appId);
            if(byId!=null){
                list.add(byId.getDeptId());
            }
        }
        if(!ObjectUtils.isEmpty(list)){
            sysDeptService.removeByIds(list);
        }
        return R.ok();
    }

}
